package com.richest.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.richest.api.dao.SysConfigMapper;
import com.richest.api.service.ISysConfigService;
import com.richest.bean.SysConfigBean;
import com.richest.cloud.CloudStorageConfig;
import com.richest.cloud.CloudStorageService;
import com.richest.cloud.QiniuCloudStorageService;
import com.richest.constant.IntegerConstant;
import com.richest.constant.StringConstant;
import com.richest.util.StringUtil;

/**
 * 
 * @author zhaohongchao 2019年3月25日 上午8:53:04
 * @email 70375739@qq.com
 */
@Service
public class SysConfigServiceImpl implements ISysConfigService {
	@Autowired
	private SysConfigMapper sysConfigMapper;

	private static SysConfigMapper mapper;
	private static CloudStorageService cloudStorageService;

	@PostConstruct
	public void init() {
		mapper = this.sysConfigMapper;
		// 获取云存储配置信息
		SysConfigBean bean = queryByKey(StringConstant.CLOUD_STORAGE_CONFIG_KEY);
		CloudStorageConfig config = JSON.parseObject(bean.getParam_value(), CloudStorageConfig.class);
		if (config.getType() == IntegerConstant.SYS_OSS_TYPE_QINIU_INT) {
			cloudStorageService = new QiniuCloudStorageService(config);
		}
	}

	@Override
	public SysConfigBean queryByPayKey(String key, String pay_key) {
		Map<String, Object> params = new HashMap<>();
		params.put("key", key);
		params.put("pay_key", pay_key);
		return mapper.queryByPayKey(params);
	}

	@Override
	public List<SysConfigBean> queryAll() {
		return mapper.queryAll();
	}

	@Override
	public List<SysConfigBean> queryAllByLimit(int page, int limit, String key, int status) {
		Map<String, Object> map = new HashMap<String, Object>();
		int row = 0;
		if (page > 0) {
			row = (page - 1) * limit;
		}
		map.put("row", row);
		map.put("limit", limit);
		if (StringUtil.isNotNullOrEmpty(key)) {
			map.put("key", key);
		}
		map.put("status", status);
		return mapper.queryAllByLimit(map);
	}

	@Override
	public int queryCount(String key, int status) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtil.isNotNullOrEmpty(key)) {
			map.put("key", key);
		}
		map.put("status", status);
		return mapper.queryCount(map);
	}

	@Override
	public synchronized void delById(Long id) {
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	public SysConfigBean queryUpdate(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public synchronized void update(SysConfigBean bean) {
		mapper.updateByPrimaryKey(bean);
	}

	@Override
	public synchronized void add(SysConfigBean bean) {
		mapper.insert(bean);
	}

	@Override
	public int checkKey(String key, Long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", key);
		if (id > 0) {
			map.put("id", id);
		}
		return mapper.checkKey(map);
	}

	@Override
	public SysConfigBean queryByKey(String key) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", key);
		return mapper.queryByKey(map);
	}

	@Override
	public synchronized void deleteByKey(String pay_key, String key) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", key);
		map.put("pay_key", pay_key);
		mapper.deleteByKey(map);
	}

	@Override
	public int checkPayKey(String pay_key, String key) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pay_key", pay_key);
		map.put("key", key);
		return mapper.checkPayKey(map);
	}

	@Override
	public CloudStorageService getCloudStorageService() {
		return cloudStorageService;
	}

}
